package com.fly.estore.dao.Impl;

import com.fly.estore.dao.BaseDAO;
import com.fly.estore.dao.ProductSpecDAO;
import com.fly.estore.domain.ProductCatalog;
import com.fly.estore.domain.ProductSpec;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Fly on 2016/4/16.
 */
@Repository("ProductSpecDAO")
public class ProductSpecDAOImpl extends BaseDAO<ProductSpec> implements ProductSpecDAO {

    public int addProductSpec(final ProductSpec spec) {
        int update = 0;
        StringBuilder sql = new StringBuilder();
        sql.append("insert into product_spec(product_spec_id, spec_name, spec_value, create_id, create_time)")
                .append("values (?, ?, ?, ?, ?)");
        update = getJdbcTemplate().update(sql.toString(), new PreparedStatementSetter() {
            public void setValues(PreparedStatement ps) throws SQLException {
                int i = 1;
                ps.setString(i++, spec.getProductSpecId());
                ps.setString(i++, spec.getSpecName());
                ps.setString(i++, spec.getSpecValues());
                ps.setString(i++, spec.getCreateId());
                ps.setString(i++, spec.getCreateTime());
            }
        });
        return update;
    }

    public int updateProductSpec(final ProductSpec spec) {
        int update = 0;
        StringBuilder sql = new StringBuilder();
        sql.append("update product_spec set ")
                .append(" spec_name = ?,")
                .append(" spec_value = ?,")
                .append(" last_update_id = ?,")
                .append(" last_update_time = ?")
                .append(" where product_spec_id = ?");
        update = getJdbcTemplate().update(sql.toString(), new PreparedStatementSetter() {
            public void setValues(PreparedStatement ps) throws SQLException {
                int i = 1;
                ps.setString(i++, spec.getSpecName());
                ps.setString(i++, spec.getSpecValues());
                ps.setString(i++, spec.getLastUpdateId());
                ps.setString(i++, spec.getLastUpdateTime());
                ps.setString(i++, spec.getProductSpecId());
            }
        });
        return update;
    }

    public int deleteById(final String productSpecId) {
        int update = 0;
        String sql = "delete from product_spec where product_spec_id = ?";
        update = getJdbcTemplate().update(sql, new PreparedStatementSetter() {
            public void setValues(PreparedStatement ps) throws SQLException {
                ps.setString(1, productSpecId);
            }
        });
        return update;
    }

    public ProductSpec getById(String productSpecId) {
        ProductSpec spec = new ProductSpec();
        String sql = "select * from product_spec where product_spec_id = ?";
        List<ProductSpec> resultList = getJdbcTemplate().query(sql, new SpecRowMapper(), new Object[]{productSpecId});
        if (!CollectionUtils.isEmpty(resultList)){
            spec = resultList.get(0);
        }
        return spec;
    }

    public List<ProductSpec> getList() {
        List<ProductSpec> specList = new ArrayList<ProductSpec>();
        String sql = "select * from product_spec";
        specList = getJdbcTemplate().query(sql, new SpecRowMapper());
        return specList;
    }

    public class SpecRowMapper implements RowMapper<ProductSpec> {

        public ProductSpec mapRow(ResultSet rs, int rowNum) throws SQLException {
            ProductSpec spec = new ProductSpec();
            spec.setProductSpecId(rs.getString("product_spec_id"));
            spec.setSpecName(rs.getString("spec_name"));
            spec.setSpecValues(rs.getString("spec_value"));
            spec.setCreateId(rs.getString("create_id"));
            spec.setCreateTime(rs.getString("create_time"));
            spec.setLastUpdateId(rs.getString("last_update_id"));
            spec.setLastUpdateTime(rs.getString("last_update_time"));
            return spec;
        }
    }
}
